package com.egao.common.module.home.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.component.elasticsearch.entity.vo.ElasticPushMsgFormVO;
import com.egao.common.component.elasticsearch.service.ElasticStatisticsService;
import com.egao.common.core.Constants;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.company.constants.CheckRecordConstants;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.constants.RecruitConstants;
import com.egao.common.module.company.entity.*;
import com.egao.common.module.company.mapper.*;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.home.entity.HomeData;
import com.egao.common.module.push.entity.PushMsg;
import com.egao.common.module.push.service.PushMsgService;
import com.egao.common.module.student.constants.StudentConstants;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.vo.StudentSignStatisticsCollegeVO;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 管理首页
 *
 * @author lym
 */
@Api(tags = "管理首页")
@RestController
@RequestMapping("${api}")
public class HomeController extends BaseController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private RecruitMapper recruitMapper;

    @Autowired
    private PreachMapper preachMapper;

    @Autowired
    private PreachCloudMapper preachCloudMapper;

    @Autowired
    private MutualApplyMapper mutualApplyMapper;

    @Autowired
    private MutualMapper mutualMapper;

    @Autowired
    private ElasticStatisticsService elasticStatisticsService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private StudentSignService studentSignService;

    @Autowired
    private PushMsgService pushMsgService;
    @Autowired
    private CollegeService collegeService;

    @PreAuthorize("hasAuthority('home')")
    @ApiOperation("生源网签接口")
    @GetMapping("/home/sign")
    public JsonResult page(Integer yearId) {
        HomeData homeData = new HomeData();

        String collegeCode = "";
        List<Student> studentList = studentService.list(new QueryWrapper<Student>()
                .select("id, degree, questionnaire_state, student_state, college_state")
                .eq("year_id", yearId)
                .eq(!StrUtil.isBlank(collegeCode), "college_code", collegeCode)
        );

        int studentWaitBkCount = 0;
        int studentWaitYjsCount = 0;
        int studentWaitCount = 0;
        int studentAuditBkCount = 0;
        int studentAuditYjsCount = 0;
        int studentAuditCount = 0;
        int studentBkCount = 0;
        int studentYjsCount = 0;
        int studentCount = studentList.size();

        for (Student student : studentList) {
            if (StudentConstants.STUDENT_DEGREE_BK.equals(student.getDegree())) {
                studentBkCount++;

                if (StudentConstants.STUDENT_STUDENT_WAIT.equals(student.getStudentState())) {
                    studentWaitBkCount++;
                    studentWaitCount++;
                }
                if (StudentConstants.STUDENT_STUDENT_SUBMIT.equals(student.getStudentState()) &&
                        StudentConstants.STUDENT_COLLEGE_WAIT.equals(student.getCollegeState())) {
                    studentAuditBkCount++;
                    studentAuditCount++;
                }
            }

            if (StudentConstants.STUDENT_DEGREE_YJS.equals(student.getDegree())) {
                studentYjsCount++;

                if (StudentConstants.STUDENT_STUDENT_WAIT.equals(student.getStudentState())) {
                    studentWaitYjsCount++;
                    studentWaitCount++;
                }
                if (StudentConstants.STUDENT_STUDENT_SUBMIT.equals(student.getStudentState()) &&
                        StudentConstants.STUDENT_COLLEGE_WAIT.equals(student.getCollegeState())) {
                    studentAuditYjsCount++;
                    studentAuditCount++;
                }
            }
        }

        homeData.setStudentWaitBkCount(studentWaitBkCount);
        homeData.setStudentWaitYjsCount(studentWaitYjsCount);
        homeData.setStudentWaitCount(studentWaitCount);

        homeData.setStudentAuditBkCount(studentAuditBkCount);
        homeData.setStudentAuditYjsCount(studentAuditYjsCount);
        homeData.setStudentAuditCount(studentAuditCount);

        homeData.setStudentBkCount(studentBkCount);
        homeData.setStudentYjsCount(studentYjsCount);
        homeData.setStudentCount(studentCount);

        HomeData signHomeData = studentSignService.homeData(yearId);
        homeData.setSignNowInvitationBkCount(signHomeData.getSignNowInvitationBkCount());
        homeData.setSignNowInvitationYjsCount(signHomeData.getSignNowInvitationYjsCount());
        homeData.setSignNowInvitationCount(signHomeData.getSignNowInvitationCount());
        homeData.setSignNowAgencyBkCount(signHomeData.getSignNowAgencyBkCount());
        homeData.setSignNowAgencyYjsCount(signHomeData.getSignNowAgencyYjsCount());
        homeData.setSignNowAgencyCount(signHomeData.getSignNowAgencyCount());
        homeData.setSignWaitBkCount(signHomeData.getSignWaitBkCount());
        homeData.setSignWaitYjsCount(signHomeData.getSignWaitYjsCount());
        homeData.setSignWaitCount(signHomeData.getSignWaitCount());

        return JsonResult.ok().setData(homeData);
    }

    @PreAuthorize("hasAuthority('home')")
    @ApiOperation("学生接口")
    @GetMapping("/home/student")
    public JsonResult student() throws IOException {
        HomeData homeData = new HomeData();

        Student student = studentService.getByUserId(getLoginUserId());
        if (student == null) {
            throw new BusinessException("学生不存在");
        }

        // 学生 就业意向状态 0提交,1未提交
        int studentQuestionnaireState = Integer.valueOf(student.getQuestionnaireState());
        homeData.setStudentQuestionnaireState(studentQuestionnaireState);

        // 学生 生源填报状态 0提交,1未提交
        int studentStudentState = Integer.valueOf(student.getStudentState());
        homeData.setStudentStudentState(studentStudentState);

        // 学生 生源填报审核状态 0通过,1待审核,2退回
        int studentCollegeState = Integer.valueOf(student.getCollegeState());
        homeData.setStudentCollegeState(studentCollegeState);

        // 学生 毕业去向登记状态 0已签约(已登记),1待审核,2退回,3未登记
        StudentSign studentSign = studentSignService.getStateValid(getLoginUserId());
        int studentSignState = 3;
        if (studentSign != null) {
            if (StudentSignConstants.SIGN_STATE_SCHOOL_PASS.equals(studentSign.getSignState())) {
                studentSignState = 0;
            }
            if (StudentSignConstants.SIGN_STATE_STUDENT_AGREED.equals(studentSign.getSignState())
                    || StudentSignConstants.SIGN_STATE_STUDENT_SUBMIT.equals(studentSign.getSignState())
                    || StudentSignConstants.SIGN_STATE_STUDENT_TERMINATION.equals(studentSign.getSignState())
            ) {
                studentSignState = 1;
            }
            if (StudentSignConstants.SIGN_STATE_COLLEGE_FAIL.equals(studentSign.getSignState())) {
                studentSignState = 2;
            }
        }
        homeData.setStudentSignState(studentSignState);

        ElasticPushMsgFormVO ep = new ElasticPushMsgFormVO();
        ep.setStudentId(student.getId());
        // 学生 推送信息 总数
        long studentPushMsgCount = elasticStatisticsService.pushCountTotal(ep);
        homeData.setStudentPushMsgCount(studentPushMsgCount);
        // 学生 推送信息 已阅读数
        long studentPushMsgReadCount = elasticStatisticsService.pushReadTotal(ep);
        homeData.setStudentPushMsgReadCount(studentPushMsgReadCount);
        // 学生 推送信息 发送简历数
        Long studentPushMsgResumeCount = elasticStatisticsService.pushResumeTotal(ep);
        homeData.setStudentPushMsgResumeCount(studentPushMsgResumeCount);
        return JsonResult.ok().setData(homeData);
    }

    @PreAuthorize("hasAuthority('home')")
    @ApiOperation("就业签接口")
    @GetMapping("/home/employment")
    public JsonResult employment() throws IOException {
        HomeData homeData = new HomeData();
        //就业 待审核 招聘信息数
        int careerWaitRecruitCount = recruitMapper.selectCount(new QueryWrapper<Recruit>().eq("state", CheckRecordConstants.CHECKING));
        homeData.setCareerWaitRecruitCount(careerWaitRecruitCount);
        //就业 待审核 校内宣讲会数
        int careerWaitPreachCount = preachMapper.selectCount(new QueryWrapper<Preach>().eq("state", CheckRecordConstants.CHECKING));
        homeData.setCareerWaitPreachCount(careerWaitPreachCount);
        //就业 待审核空中宣讲会数
        int careerWaitPreachCloudCount = preachCloudMapper.selectCount(new QueryWrapper<PreachCloud>().eq("state", CheckRecordConstants.CHECKING));
        homeData.setCareerWaitPreachCloudCount(careerWaitPreachCloudCount);
        //就业 待审核校内双选会报名单位数
        int careerWaitMutualCount = mutualApplyMapper.selectCount(new QueryWrapper<MutualApply>().eq("state", CheckRecordConstants.CHECKING));
        homeData.setCareerWaitMutualCount(careerWaitMutualCount);
        String startTime = DateUtil.today() + " 00:00:00";
        String endTime = DateUtil.today() + " 23:59:59";
        //就业 今日 发布招聘信息数
        int careerNowRecruitCount = recruitMapper.selectCount(new QueryWrapper<Recruit>().apply("create_time >= '" + startTime + "'").apply("create_time <= '" + endTime + "'"));
        homeData.setCareerNowRecruitCount(careerNowRecruitCount);
        //就业 今日 举办的校内宣讲会数
        int careerNowPreachCount = preachMapper.selectCount(new QueryWrapper<Preach>().apply("hold_time >= '" + startTime + "'").apply("hold_time <= '" + endTime + "'"));
        homeData.setCareerNowPreachCount(careerNowPreachCount);
        //就业 今日 举办的空中宣讲会数
        int careerNowPreachCloudCount = preachCloudMapper.selectCount(new QueryWrapper<PreachCloud>().apply("hold_time >= '" + startTime + "'").apply("hold_time <= '" + endTime + "'"));
        homeData.setCareerNowPreachCloudCount(careerNowPreachCloudCount);
        //就业 今日 举办的校内双选会数
        int careerNowMutualCount = mutualMapper.selectCount(new QueryWrapper<Mutual>().apply("start_time >= '" + startTime + "'").apply("start_time <= '" + endTime + "'"));
        homeData.setCareerNowMutualCount(careerNowMutualCount);
        //就业 今日 今日推送信息总数
        ElasticPushMsgFormVO ep = new ElasticPushMsgFormVO();
        String applyTime = DateUtil.today() + "~" + DateUtil.today();
        ep.setApplyTime(applyTime);
        // 学院
        if (!StrUtil.isBlank(getLoginUser().getCollegeCode())) {
            ep.setCollegeCode(getLoginUser().getCollegeCode());
        }

        long careerNowPushMsgCount = elasticStatisticsService.pushCountTotal(ep);
        homeData.setCareerNowPushMsgCount(careerNowPushMsgCount);
        //就业 今日 今日推送信息已阅读数
        long careerNowPushMsgReadCount = elasticStatisticsService.pushReadTotal(ep);
        homeData.setCareerNowPushMsgReadCount(careerNowPushMsgReadCount);
        //就业 今日 今日推送信息已发送简历数
        long careerNowPushMsgResumeCount = elasticStatisticsService.pushResumeTotal(ep);
        homeData.setCareerNowPushMsgResumeCount(careerNowPushMsgResumeCount);
        return JsonResult.ok().setData(homeData);
    }

    @PreAuthorize("hasAuthority('home')")
    @ApiOperation("就业签接口")
    @GetMapping("/home/company")
    public JsonResult company() throws IOException {
        Company company = companyService.getByUserId(getLoginUserId());
        if (company == null) {
            return JsonResult.error("无权限");
        }
        HomeData homeData = new HomeData();
        String nowTime = DateUtil.now();
        // 单位 正在发布中的招聘信息 (未过期的招聘信息,审核通过的)
        int companyRecruitCount = recruitMapper.selectCount(new QueryWrapper<Recruit>().apply("valid_time >= '" + nowTime + "'").eq("state", CheckRecordConstants.CHECK_SUCCESS).eq("company_id", company.getId()));
        homeData.setCompanyRecruitCount(companyRecruitCount);
        // 单位 校内宣讲会 待开始 (审核通过的)
        int companyPreachWaitCount = preachMapper.selectCount(new QueryWrapper<Preach>().apply("hold_time >= '" + nowTime + "'").eq("state", CheckRecordConstants.CHECK_SUCCESS).eq("company_id", company.getId()));
        homeData.setCompanyPreachWaitCount(companyPreachWaitCount);
        // 单位 校内宣讲会 已举办 (审核通过的)
        int companyPreachHeldCount = preachMapper.selectCount(new QueryWrapper<Preach>().eq("state", CheckRecordConstants.CHECK_SUCCESS).eq("company_id", company.getId()));
        homeData.setCompanyPreachHeldCount(companyPreachHeldCount);
        // 单位 校内宣讲会 总数 (审核通过的)
        int companyPreachCount = preachMapper.selectCount(new QueryWrapper<Preach>().eq("company_id", company.getId()));
        homeData.setCompanyPreachCount(companyPreachCount);
        // 单位 空中宣讲会 待开始 (审核通过的)
        int companyPreachCloudWaitCount = preachCloudMapper.selectCount(new QueryWrapper<PreachCloud>().apply("hold_time >= '" + nowTime + "'").eq("state", CheckRecordConstants.CHECK_SUCCESS).eq("company_id", company.getId()));
        homeData.setCompanyPreachCloudWaitCount(companyPreachCloudWaitCount);
        // 单位 空中宣讲会 已举办 (审核通过的)
        int companyPreachCloudHeldCount = preachCloudMapper.selectCount(new QueryWrapper<PreachCloud>().eq("state", CheckRecordConstants.CHECK_SUCCESS).eq("company_id", company.getId()));
        homeData.setCompanyPreachCloudHeldCount(companyPreachCloudHeldCount);
        // 单位 空中宣讲会 总数 (审核通过的)
        int companyPreachCloudCount = preachCloudMapper.selectCount(new QueryWrapper<PreachCloud>().eq("company_id", company.getId()));
        homeData.setCompanyPreachCloudCount(companyPreachCloudCount);
        // 单位 校内双选会 报名参加过的个数
        int companyMutualCount = mutualApplyMapper.selectCount(new QueryWrapper<MutualApply>().eq("company_id", company.getId()));
        homeData.setCompanyMutualCount(companyMutualCount);
        // 单位 信息推送 推送给学生的总数
        ElasticPushMsgFormVO ep = new ElasticPushMsgFormVO();
        ep.setCompanyId(company.getId());
        long companyPushMsgCount = elasticStatisticsService.pushCountTotal(ep);
        homeData.setCompanyPushMsgCount(companyPushMsgCount);
        return JsonResult.ok().setData(homeData);
    }

    @GetMapping("/home")
    public JsonResult home(Integer year) throws IOException {
        int pass = 1;
        DateTime begin = DateUtil.beginOfMonth(new Date());
        DateTime end = DateUtil.endOfMonth(new Date());

        DateTime lastBegin = DateUtil.offsetMonth(begin, -12);
        DateTime lastEnd = DateUtil.offsetMonth(end, -12);

        // 单位
        int companyTotal = companyService.count(new LambdaQueryWrapper<Company>()
                .select(Company::getId)
                .eq(Company::getState, pass)
        );
        int companyMonthTotal = companyService.count(new LambdaQueryWrapper<Company>()
                .select(Company::getId)
                .eq(Company::getState, pass)
                .ge(Company::getCreateTime, begin)
                .le(Company::getCreateTime, end)
        );
        int companyLastMonthTotal = companyService.count(new LambdaQueryWrapper<Company>()
                .select(Company::getId)
                .eq(Company::getState, pass)
                .ge(Company::getCreateTime, lastBegin)
                .le(Company::getCreateTime, lastEnd)
        );
        Double companyRatio = companyLastMonthTotal != 0 ? NumberUtil.div((companyMonthTotal - companyLastMonthTotal), companyLastMonthTotal, 4) : 0.00;
        boolean companyUp = true;
        if (companyRatio < 0) {
            companyUp = false;
            companyRatio = NumberUtil.mul(companyRatio, companyRatio);
        }

        // 招聘信息
        int recruitTotal = recruitMapper.selectCount(new LambdaQueryWrapper<Recruit>()
                .select(Recruit::getId)
                .eq(Recruit::getState, pass)
        );
        int recruitMonthTotal = recruitMapper.selectCount(new LambdaQueryWrapper<Recruit>()
                .select(Recruit::getId)
                .eq(Recruit::getState, pass)
                .ge(Recruit::getCreateTime, begin)
                .le(Recruit::getCreateTime, end)
        );
        int recruitLastMonthTotal = recruitMapper.selectCount(new LambdaQueryWrapper<Recruit>()
                .select(Recruit::getId)
                .eq(Recruit::getState, pass)
                .ge(Recruit::getCreateTime, lastBegin)
                .le(Recruit::getCreateTime, lastEnd)
        );

        Double recruitRatio = recruitLastMonthTotal != 0 ? NumberUtil.div((recruitMonthTotal - recruitLastMonthTotal), recruitLastMonthTotal, 4) : 0.00;
        boolean recruitUp = true;
        if (recruitRatio < 0) {
            recruitUp = false;
            recruitRatio = NumberUtil.mul(recruitRatio, recruitRatio);
        }

        // 校内宣讲会
        int preachTotal = preachMapper.selectCount(new LambdaQueryWrapper<Preach>()
                .select(Preach::getId)
                .eq(Preach::getState, pass)
        );
        int preachMonthTotal = preachMapper.selectCount(new LambdaQueryWrapper<Preach>()
                .select(Preach::getId)
                .eq(Preach::getState, pass)
                .ge(Preach::getCreateTime, begin)
                .le(Preach::getCreateTime, end)
        );
        int preachLastMonthTotal = preachMapper.selectCount(new LambdaQueryWrapper<Preach>()
                .select(Preach::getId)
                .eq(Preach::getState, pass)
                .ge(Preach::getCreateTime, lastBegin)
                .le(Preach::getCreateTime, lastEnd)
        );
        Double preachRatio = preachLastMonthTotal != 0 ? NumberUtil.div((preachMonthTotal - preachLastMonthTotal), preachLastMonthTotal, 4) : 0.00;
        boolean preachUp = true;
        if (preachRatio < 0) {
            preachUp = false;
            preachRatio = NumberUtil.mul(preachRatio, preachRatio);
        }

        // 校内双选会
        int mutualTotal = mutualMapper.selectCount(new LambdaQueryWrapper<Mutual>()
                .select(Mutual::getId)
        );
        int mutualApplyTotal = mutualApplyMapper.selectCount(new LambdaQueryWrapper<MutualApply>()
                .select(MutualApply::getId)
                .eq(MutualApply::getState, pass)
        );
        int mutualMonthTotal = mutualMapper.selectCount(new LambdaQueryWrapper<Mutual>()
                .select(Mutual::getId)
                .ge(Mutual::getCreateTime, begin)
                .le(Mutual::getCreateTime, end)
        );
        int mutualLastMonthTotal = mutualMapper.selectCount(new LambdaQueryWrapper<Mutual>()
                .select(Mutual::getId)
                .ge(Mutual::getCreateTime, lastBegin)
                .le(Mutual::getCreateTime, lastEnd)
        );
        Double mutualRatio = mutualLastMonthTotal != 0 ? NumberUtil.div((mutualMonthTotal - mutualLastMonthTotal), mutualLastMonthTotal, 4) : 0.00;
        boolean mutualUp = true;
        if (mutualRatio < 0) {
            mutualUp = false;
            mutualRatio = NumberUtil.mul(mutualRatio, mutualRatio);
        }

        // 推送
        ElasticPushMsgFormVO epYear = new ElasticPushMsgFormVO();
        epYear.setYearId(year);

        ElasticPushMsgFormVO epToday = new ElasticPushMsgFormVO();
        epToday.setYearId(year);
        epToday.setApplyTime(DateUtil.today() + "~" + DateUtil.today());

        ElasticPushMsgFormVO epYesterday = new ElasticPushMsgFormVO();
        epYesterday.setYearId(year);
        epYesterday.setApplyTime(DateUtil.formatDate(DateUtil.yesterday()) + "~" + DateUtil.formatDate(DateUtil.yesterday()));

        long pushTotal = elasticStatisticsService.pushCountTotal(epYear);
        long pushTodayTotal = elasticStatisticsService.pushCountTotal(epToday);
        long pushYesterdayTotal = elasticStatisticsService.pushCountTotal(epYesterday);

        long pushStudentTotal = elasticStatisticsService.pushStudentTotal(epYear);
        long pushStudentTodayTotal = elasticStatisticsService.pushStudentTotal(epToday);
        long pushStudentYesterdayTotal = elasticStatisticsService.pushStudentTotal(epYesterday);

        long pushReadTotal = elasticStatisticsService.pushReadTotal(epYear);
        long pushReadTodayTotal = elasticStatisticsService.pushReadTotal(epToday);
        long pushReadYesterdayTotal = elasticStatisticsService.pushReadTotal(epYesterday);

        long pushResumeTotal = elasticStatisticsService.pushResumeTotal(epYear);
        long pushResumeTodayTotal = elasticStatisticsService.pushResumeTotal(epToday);
        long pushResumeYesterdayTotal = elasticStatisticsService.pushResumeTotal(epYesterday);

        // 网签
        StudentSignStatisticsCollegeVO statisticsCollegeVO = studentSignService.signCount(year);
        int studentCount = studentService.count(new LambdaQueryWrapper<Student>().eq(Student::getYearId, year));
        int studentSignCount = statisticsCollegeVO.getStudentSignCount();
        Double signRatio = NumberUtil.div(studentSignCount, studentCount, 4);

        int xyjyCount = statisticsCollegeVO.getXyjyCount();
        Double xyjyStudentRatio = NumberUtil.div(xyjyCount, studentCount, 4);
        Double xyjySignRatio = NumberUtil.div(xyjyCount, studentSignCount, 4);

        int lhjyCount = statisticsCollegeVO.getLhjyCount();
        Double lhjyStudentRatio = NumberUtil.div(lhjyCount, studentCount, 4);
        Double lhjySignRatio = NumberUtil.div(lhjyCount, studentSignCount, 4);

        int zzcyCount = statisticsCollegeVO.getZzcyCount();
        Double zzcyStudentRatio = NumberUtil.div(zzcyCount, studentCount, 4);
        Double zzcySignRatio = NumberUtil.div(zzcyCount, studentSignCount, 4);

        int zyzyCount = statisticsCollegeVO.getZyzyCount();
        Double zyzyStudentRatio = NumberUtil.div(zyzyCount, studentCount, 4);
        Double zyzySignRatio = NumberUtil.div(zyzyCount, studentSignCount, 4);

        int sxCount = statisticsCollegeVO.getSxCount();
        Double sxStudentRatio = NumberUtil.div(sxCount, studentCount, 4);
        Double sxSignRatio = NumberUtil.div(sxCount, studentSignCount, 4);

        int cgcjCount = statisticsCollegeVO.getCgcjCount();
        Double cgcjStudentRatio = NumberUtil.div(cgcjCount, studentCount, 4);
        Double cgcjSignRatio = NumberUtil.div(cgcjCount, studentSignCount, 4);

        int djyCount = statisticsCollegeVO.getDjyCount();
        Double djyStudentRatio = NumberUtil.div(djyCount, studentCount, 4);
        Double djySignRatio = NumberUtil.div(djyCount, studentSignCount, 4);


        return JsonResult.ok()
                .put("companyTotal", NumberUtil.decimalFormat(",###", companyTotal))
                .put("companyMonthTotal", NumberUtil.decimalFormat(",###", companyMonthTotal))
                .put("companyLastMonthTotal", NumberUtil.decimalFormat(",###", companyLastMonthTotal))
                .put("companyRatio", NumberUtil.decimalFormat("#.##%", companyRatio))
                .put("companyUp", companyUp)

                .put("recruitTotal", NumberUtil.decimalFormat(",###", recruitTotal))
                .put("recruitMonthTotal", NumberUtil.decimalFormat(",###", recruitMonthTotal))
                .put("recruitLastMonthTotal", NumberUtil.decimalFormat(",###", recruitLastMonthTotal))
                .put("recruitRatio", NumberUtil.decimalFormat("#.##%", recruitRatio))
                .put("recruitUp", recruitUp)

                .put("preachTotal", NumberUtil.decimalFormat(",###", preachTotal))
                .put("preachMonthTotal", NumberUtil.decimalFormat(",###", preachMonthTotal))
                .put("preachLastMonthTotal", NumberUtil.decimalFormat(",###", preachLastMonthTotal))
                .put("preachRatio", NumberUtil.decimalFormat("#.##%", preachRatio))
                .put("preachUp", preachUp)

                .put("mutualTotal", NumberUtil.decimalFormat(",###", mutualTotal))
                .put("mutualMonthTotal", NumberUtil.decimalFormat(",###", mutualMonthTotal))
                .put("mutualLastMonthTotal", NumberUtil.decimalFormat(",###", mutualLastMonthTotal))
                .put("mutualRatio", NumberUtil.decimalFormat("#.##%", mutualRatio))
                .put("mutualUp", mutualUp)
                .put("mutualApplyTotal", NumberUtil.decimalFormat(",###", mutualApplyTotal))

                .put("pushTotal", NumberUtil.decimalFormat(",###", pushTotal))
                .put("pushTodayTotal", NumberUtil.decimalFormat(",###", pushTodayTotal))
                .put("pushYesterdayTotal", NumberUtil.decimalFormat(",###", pushYesterdayTotal))

                .put("pushStudentTotal", NumberUtil.decimalFormat(",###", pushStudentTotal))
                .put("pushStudentTodayTotal", NumberUtil.decimalFormat(",###", pushStudentTodayTotal))
                .put("pushStudentYesterdayTotal", NumberUtil.decimalFormat(",###", pushStudentYesterdayTotal))

                .put("pushReadTotal", NumberUtil.decimalFormat(",###", pushReadTotal))
                .put("pushReadTodayTotal", NumberUtil.decimalFormat(",###", pushReadTodayTotal))
                .put("pushReadYesterdayTotal", NumberUtil.decimalFormat(",###", pushReadYesterdayTotal))

                .put("pushResumeTotal", NumberUtil.decimalFormat(",###", pushResumeTotal))
                .put("pushResumeTodayTotal", NumberUtil.decimalFormat(",###", pushResumeTodayTotal))
                .put("pushResumeYesterdayTotal", NumberUtil.decimalFormat(",###", pushResumeYesterdayTotal))

                .put("studentCount", NumberUtil.decimalFormat(",###", studentCount))
                .put("studentSignCount", NumberUtil.decimalFormat(",###", studentSignCount))
                .put("signRatio", NumberUtil.decimalFormat("#.##%", signRatio))

                .put("xyjyCount", NumberUtil.decimalFormat(",###", xyjyCount))
                .put("xyjySignRatio", NumberUtil.decimalFormat("#.##%", xyjySignRatio))
                .put("xyjyStudentRatio", NumberUtil.decimalFormat("#.##%", xyjyStudentRatio))

                .put("lhjyCount", NumberUtil.decimalFormat(",###", lhjyCount))
                .put("lhjySignRatio", NumberUtil.decimalFormat("#.##%", lhjySignRatio))
                .put("lhjyStudentRatio", NumberUtil.decimalFormat("#.##%", lhjyStudentRatio))

                .put("zzcyCount", NumberUtil.decimalFormat(",###", zzcyCount))
                .put("zzcySignRatio", NumberUtil.decimalFormat("#.##%", zzcySignRatio))
                .put("zzcyStudentRatio", NumberUtil.decimalFormat("#.##%", zzcyStudentRatio))

                .put("zyzyCount", NumberUtil.decimalFormat(",###", zyzyCount))
                .put("zyzySignRatio", NumberUtil.decimalFormat("#.##%", zyzySignRatio))
                .put("zyzyStudentRatio", NumberUtil.decimalFormat("#.##%", zyzyStudentRatio))

                .put("sxCount", NumberUtil.decimalFormat(",###", sxCount))
                .put("sxSignRatio", NumberUtil.decimalFormat("#.##%", sxSignRatio))
                .put("sxStudentRatio", NumberUtil.decimalFormat("#.##%", sxStudentRatio))

                .put("cgcjCount", NumberUtil.decimalFormat(",###", cgcjCount))
                .put("cgcjSignRatio", NumberUtil.decimalFormat("#.##%", cgcjSignRatio))
                .put("cgcjStudentRatio", NumberUtil.decimalFormat("#.##%", cgcjStudentRatio))

                .put("djyCount", NumberUtil.decimalFormat(",###", djyCount))
                .put("djySignRatio", NumberUtil.decimalFormat("#.##%", djySignRatio))
                .put("djyStudentRatio", NumberUtil.decimalFormat("#.##%", djyStudentRatio))
                ;

    }


    @ApiOperation("生源统计")
    @GetMapping("/home/students")
    public JsonResult student(Integer year) {
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>()
                .select(Student::getXl).eq(Student::getYearId, year)
        );

        int studentTotal = studentList.size();
        long bkTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
        long bkrxwTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_XL_BKRXW.equals(d.getXl())).count();
        long ssTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_XL_SS.equals(d.getXl())).count();
        long bsTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_XL_BS.equals(d.getXl())).count();
        long yjsTotal = ssTotal + bsTotal;

        return JsonResult.ok().put("studentTotal", studentTotal)
                .put("bkTotal", bkTotal)
                .put("bkrxwTotal", bkrxwTotal)
                .put("yjsTotal", yjsTotal);
    }

    @ApiOperation("就业意向统计")
    @GetMapping("/home/inten")
    public JsonResult inten(Integer year) {
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>()
                .select(Student::getXl, Student::getDegree, Student::getQuestionnaireState, Student::getInten)
                .eq(Student::getYearId, year)
                .eq(Student::getQuestionnaireState, StudentConstants.STUDENT_QUESTIONNAIRE_SUBMIT)
                .ne(Student::getXl, StudentConstants.STUDENT_XL_BKRXW)
        );

        int studentTotal = studentList.size();
        long studentBkTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_DEGREE_BK.equals(d.getDegree())).count();
        long studentYjsTotal = studentList.stream().filter(d -> StudentConstants.STUDENT_DEGREE_YJS.equals(d.getDegree())).count();
        Double studentBkRatio = NumberUtil.div(studentBkTotal, studentTotal, 4);
        Double studentYjsRatio = NumberUtil.div(studentYjsTotal, studentTotal, 4);

        List<Student> qzList = studentList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).collect(Collectors.toList());
        int qzTotal = qzList.size();
        long qzBkTotal = qzList.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
        long qzYjsTotal = qzList.stream().filter(d -> StudentConstants.STUDENT_DEGREE_YJS.equals(d.getDegree())).count();
        Double qzBkRatio = NumberUtil.div(qzBkTotal, qzTotal, 4);
        Double qzYjsRatio = NumberUtil.div(qzYjsTotal, qzTotal, 4);

        List<Student> sxList = studentList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).collect(Collectors.toList());
        int sxTotal = sxList.size();
        long sxBkTotal = sxList.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
        long sxYjsTotal = sxList.stream().filter(d -> StudentConstants.STUDENT_DEGREE_YJS.equals(d.getDegree())).count();
        Double sxBkRatio = NumberUtil.div(sxBkTotal, sxTotal, 4);
        Double sxYjsRatio = NumberUtil.div(sxYjsTotal, sxTotal, 4);

        List<Student> kgList = studentList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).collect(Collectors.toList());
        int kgTotal = kgList.size();
        long kgBkTotal = kgList.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
        long kgYjsTotal = kgList.stream().filter(d -> StudentConstants.STUDENT_DEGREE_YJS.equals(d.getDegree())).count();
        Double kgBkRatio = NumberUtil.div(kgBkTotal, kgTotal, 4);
        Double kgYjsRatio = NumberUtil.div(kgYjsTotal, kgTotal, 4);

        List<Student> kyList = studentList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考研")
                && StudentConstants.STUDENT_XL_BK.equals(d.getXl())).collect(Collectors.toList());
        int kyTotal = kyList.size();


        List<Student> twoList = studentService.list(new LambdaQueryWrapper<Student>()
                .select(Student::getInten)
                .eq(Student::getYearId, year)
                .eq(Student::getQuestionnaireState, StudentConstants.STUDENT_QUESTIONNAIRE_SUBMIT)
                .eq(Student::getXl, StudentConstants.STUDENT_XL_BKRXW)
        );
        long twoQz = twoList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).count();
        long twoSx = twoList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).count();
        long twoKg = twoList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).count();
        long twoKy = twoList.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考研")).count();

        return JsonResult.ok()
                .put("studentTotal", NumberUtil.decimalFormat(",###", studentTotal))
                .put("studentBkTotal", NumberUtil.decimalFormat(",###", studentBkTotal))
                .put("studentYjsTotal", NumberUtil.decimalFormat(",###", studentYjsTotal))
                .put("studentBkRatio", NumberUtil.decimalFormat("#.##%", studentBkRatio))
                .put("studentYjsRatio", NumberUtil.decimalFormat("#.##%", studentYjsRatio))

                .put("qzTotal", NumberUtil.decimalFormat(",###", qzTotal))
                .put("qzBkTotal", NumberUtil.decimalFormat(",###", qzBkTotal))
                .put("qzYjsTotal", NumberUtil.decimalFormat(",###", qzYjsTotal))
                .put("qzBkRatio", NumberUtil.decimalFormat("#.##%", qzBkRatio))
                .put("qzYjsRatio", NumberUtil.decimalFormat("#.##%", qzYjsRatio))

                .put("sxTotal", NumberUtil.decimalFormat(",###", sxTotal))
                .put("sxBkTotal", NumberUtil.decimalFormat(",###", sxBkTotal))
                .put("sxYjsTotal", NumberUtil.decimalFormat(",###", sxYjsTotal))
                .put("sxBkRatio", NumberUtil.decimalFormat("#.##%", sxBkRatio))
                .put("sxYjsRatio", NumberUtil.decimalFormat("#.##%", sxYjsRatio))

                .put("kgTotal", NumberUtil.decimalFormat(",###", kgTotal))
                .put("kgBkTotal", NumberUtil.decimalFormat(",###", kgBkTotal))
                .put("kgYjsTotal", NumberUtil.decimalFormat(",###", kgYjsTotal))
                .put("kgBkRatio", NumberUtil.decimalFormat("#.##%", kgBkRatio))
                .put("kgYjsRatio", NumberUtil.decimalFormat("#.##%", kgYjsRatio))

                .put("kyTotal", NumberUtil.decimalFormat(",###", kyTotal))

                .put("twoQz", NumberUtil.decimalFormat(",###", twoQz))
                .put("twoSx", NumberUtil.decimalFormat(",###", twoSx))
                .put("twoKg", NumberUtil.decimalFormat(",###", twoKg))
                .put("twoKy", NumberUtil.decimalFormat(",###", twoKy))
                ;
    }

    @PreAuthorize("hasAuthority('home')")
    @ApiOperation("困难生推送统计")
    @GetMapping("/home/push/difficult")
    public JsonResult difficult(Integer year) {
        String collegeCode = getLoginUser().getCollegeCode();
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>()
                .select(Student::getId)
                .eq(Student::getYearId, year)
                .eq(!StrUtil.isBlank(collegeCode), Student::getCollegeCode, collegeCode)
                .ne(Student::getKnslb, "0"));
        if (studentList.size() == 0) {
            return JsonResult.ok();
        }

        List<Integer> studentIds = studentList.stream().map(Student::getId).collect(Collectors.toList());
        List<PushMsg> list = pushMsgService.list(new LambdaQueryWrapper<PushMsg>()
                        .eq(PushMsg::getCompanyFlag, "0")
                        .eq(PushMsg::getAdminFlag, "0")
                        .eq(PushMsg::getYearId, year)
                        .in(PushMsg::getStudentId, studentIds));

        int difficultStudentCount = studentIds.size();
        int difficultPushCount = list.size();
        long difficultReadCount = list.stream().filter(d -> "0".equals(d.getReadFlag())).count();
        long difficultResumeCount = list.stream().filter(d -> "0".equals(d.getResumeFlag())).count();

        return JsonResult.ok().put("difficultStudentCount", difficultStudentCount)
                .put("difficultPushCount", difficultPushCount)
                .put("difficultReadCount", difficultReadCount)
                .put("difficultResumeCount", difficultResumeCount);
    }
}
